<?php

/**
 * Description of Column
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace OverFish\Controls\Grid;

class Column extends \Nette\Application\UI\Control {

    /** @var string */
    private $title;
    /** @var string */
    private $attribute;
    /** @var bool */
    private $sortable = false;
    /** @var callback */
    private $renderer = null;
    /** @var string|callable */
    private $cellClass = null;
    /** @var string */
    private $dateTimeFormat = "j.n.Y G:i";

    /**
     * Get sorting
     * @return string|null asc, desc or null
     */
    public function getSorting() {
        $grid = $this->getGrid();
        if ($grid->sortColumn === $this->getName()) {
            return $grid->sortType;
        } else {
            return null;
        }
    }

    /**
     * Get date/time format
     * @return string
     */
    public function getDateTimeFormat() {
        return $this->dateTimeFormat;
    }

    /**
     * Set date/time format
     * @param string datetime format
     * @return Column
     */
    public function setDateTimeFormat($dateTimeFormat) {
        $this->dateTimeFormat = $dateTimeFormat;
        return $this;
    }

    /**
     * Render boolean
     * @param bool value
     */
    public static function renderBoolean($value) {
        $icon = $value ? "check" : "closethick";
        echo '<span class="ui-icon ui-icon-' . $icon . '"></span>';
    }

    /**
     * Get grid
     * @return Grid
     */
    public function getGrid() {
        return $this->getParent()->getParent();
    }

    /**
     * Render datetime
     * @param Datetime value
     * @param string datetime format
     */
    public static function renderDateTime($value, $format) {
        echo $value->format($format);
    }

    /**
     * Default cell renderer
     * @param mixed $record
     * @param Column $column
     */
    public function defaultCellRenderer($record, $column) {
        $name = $column->getName();
        $value = $record->$name;

        // boolean
        if (is_bool($value)) {
            self::renderBoolean($value);

            // date
        } elseif ($value instanceof \DateTime) {
            self::renderDateTime($value, $this->dateTimeFormat);

            // other
        } else {
            echo $value;
        }
    }

    /**
     * Render cell
     * @param mixed record
     */
    public function renderCell($record) {
        call_user_func($this->renderer ? : array($this, "defaultCellRenderer"), $record, $this);
    }

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
        return $this;
    }

    public function isSortable() {
        return $this->sortable;
    }

    public function setSortable($sortable) {
        $this->sortable = $sortable;
        return $this;
    }

    public function getRenderer() {
        return $this->renderer;
    }

    public function setRenderer($renderer) {
        $this->renderer = $renderer;
        return $this;
    }

    public function getCellClass($iterator, $row) {
        if (is_callable($this->cellClass)) {
            return call_user_func($this->cellClass, $iterator, $row);
        } elseif (is_string($this->cellClass)) {
            return $this->cellClass;
        } else {
            return null;
        }
    }

    public function setCellClass($cellClass) {
        $this->cellClass = $cellClass;
        return $this;
    }

}

